﻿using AutoMapper;
using JXC.Application.Dtos;
using JXC.Core.Models;
using JXC.Core.Repository;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JXC.EFCore.Models;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JXC.WebAPI.Controllers
{
    /// <summary>
    /// 审核控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class AuditLoggingController : ControllerBase
    {

        private readonly IRepository<AuditLogging, Guid> _repository;
        private readonly IMapper _mapper;

        public AuditLoggingController(
            IRepository<AuditLogging, Guid> repository,

            IMapper mapper
            )
        {

            _repository = repository;
            _mapper = mapper;
        }
        // GET: api/<AuditLoggingController>
        /// <summary>
        /// 获取当前用户需要审核的文件
        /// 用户id，
        /// 且标识为审核中的 文件
        /// 如果审核通过，把这个文件的顺序为二的设置为审核中
        /// 
        /// </summary>
        /// <returns></returns>
        [Route("getAudit")]
        [HttpPost]
        public async Task<IActionResult> Get([FromBody] Class1 auditLoggingDto)
        {

            try
            {
                List<AuditLogging> auditLoggings = await _repository.GetAllListAsync(p => p.UserInfosId == Guid.Parse(auditLoggingDto.UserInfosId) && p.AuditStatus == (int)AuditStatus.InReview);
                List<AuditLoggingDto> auditLoggingDtos = _mapper.Map<List<AuditLoggingDto>>(auditLoggings);

                return Ok(auditLoggingDtos);
            }
            catch (Exception e)
            {

                return BadRequest("发生了未知的错误" + e);
            }





        }

        // GET api/<AuditLoggingController>/5
        [HttpGet("{id}")]
        public async Task<IActionResult> Get(Guid id)
        {
            try
            {
                AuditLogging auditLogging = await _repository.SingleAsync(p => p.WenJianId == id);
                if (auditLogging == null)
                {
                    return BadRequest("查询失败");
                }
                else
                {
                    AuditLoggingDto auditLoggingDto = _mapper.Map<AuditLoggingDto>(auditLogging);
                    return Ok(auditLoggingDto);
                }
            }
            catch (Exception)
            {

                throw;
            }


        }

        /// <summary>
        /// 给审核记录表添加数据  标注审核顺序
        /// </summary>
        /// <param name="auditLoggingDto"></param>
        /// <returns></returns>
        // POST api/<AuditLoggingController>
        [HttpPost]
        public IActionResult Post([FromBody] AddAuditLoggingDto addAuditLoggingDto)
        {
            int num = 1;//代表审核顺序
            List<AuditLogging> auditLoggings = new List<AuditLogging>();
            try
            {
                foreach (var item in addAuditLoggingDto.UserInfosIdList)
                {
                    if (num == 1)
                    {
                        AuditLogging auditLogging = new AuditLogging
                        {
                            UserInfosId = item,
                            WenJianId = addAuditLoggingDto.WenJianId,
                            Reorder = num,
                            AuditStatus = (int)AuditStatus.InReview,
                            CreateTime = DateTime.Now
                        };
                        auditLoggings.Add(auditLogging);
                    }
                    else
                    {
                        AuditLogging auditLogging = new AuditLogging
                        {
                            UserInfosId = item,
                            WenJianId = addAuditLoggingDto.WenJianId,
                            Reorder = num,
                            AuditStatus = (int)AuditStatus.CheckPending,
                            CreateTime = DateTime.Now
                        };
                        auditLoggings.Add(auditLogging);
                    }
                    num++;
                }
                _repository.AddsAsync(auditLoggings);
                return Ok();
            }
            catch (Exception e)
            {
                return BadRequest("发生了未知错误" + e);
            }

        }

        /// <summary>
        /// 修改记录的状态，
        /// 审核通过，修改状态为2，
        /// 把下一个人的状态设置为1
        /// 这个条记录，顺序为2的就是下一个人
        /// 需要的参数是这条记录的id，
        /// 这个文件的id，顺序为2
        /// 
        /// 参数
        /// 标识id，
        /// 文件id，
        /// 这条记录的顺序
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        // PUT api/<AuditLoggingController>/5
        [HttpPut]
        public async Task<IActionResult> Put([FromBody] AuditLoggingDto auditLoggingDto)
        {
            try
            {
                AuditLogging auditLogging = await _repository.UpdateAsync(_mapper.Map<AuditLogging>(auditLoggingDto));
                if (auditLogging==null)
                {
                    return BadRequest();
                }
                else {
                    int num= auditLoggingDto.Reorder + 1;
                    Guid wenJianId = auditLoggingDto.WenJianId;
                    List<AuditLogging> auditLoggings = await _repository.GetAllListAsync(p => p.Reorder == 2 && p.WenJianId == wenJianId);
                    foreach (var item in auditLoggings)
                    {
                        item.AuditStatus = 1;
                        await _repository.UpdateAsync(item);
                    }
                    
                }
                return Ok(auditLogging);
            }
            catch (Exception)
            {

                throw;
            }
         
        }

        // DELETE api/<AuditLoggingController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
